/**
 * 文件名称:  ParamDefController.java
 * 文件序号:  序号
 * 内容描述:  文件简单描述，多行换行书写
 * 版权声明:  山东鸿蝶信息技术有限公司
 * 创建用户:  zhangkai
 * 创建日期:  2017/1/16 08:21
 * 修改用户:  无
 * 修改日期:  无
 * 修改说明:  无
 * 文件版本:  V1.0
 * 文件路径:
 */

package com.hongdee.base.brand.controller;


import com.hongdee.base.brand.entity.TBcBrand;
import com.hongdee.base.brand.service.IBrandService;
import com.hongdee.base.entity.ResultEntity;
import com.hongdee.common.util.EntityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 名  称: BrandController
 * 类  型: class
 * 描  述:
 * 路  径:com.hongdee.base.brand.controller
 * 作  者:zhangkai
 * 日  期:2017/1/16 08:21
 */
@RestController
@RequestMapping("/base/brand")
public class BrandController {

    @Autowired
    private IBrandService brandService;

    /**
     * @param start       起始记录
     * @param limit       页面大小
     * @param brandCode   品牌编码
     * @param brandName   品牌名称
     * @param pCode 生产厂家
     * @return Map
     * @throws Exception
     * @desc 通过 JDBCDao分页查询
     * @author 张国凯
     */
    @RequestMapping("/page")
    @ResponseBody
    public Map page(@RequestParam(name = "uid", defaultValue = "") String uid,
                    @RequestParam(name = "token", defaultValue = "") String token,
                    @RequestParam(name = "start", defaultValue = "0") String start,
                    @RequestParam(name = "limit", defaultValue = "50") String limit,
                    @RequestParam(name = "bCode", defaultValue = "") String brandCode,
                    @RequestParam(name = "bName", defaultValue = "") String brandName,
                    @RequestParam(name = "pCode", defaultValue = "") String pCode
    ) throws Exception {

        Map map = brandService.page(start, limit, brandCode, brandName, pCode);
        map.put("success", true);
        map.put("msg", "分页查询成功！");
        return map;
    }

    /**
     * @param uid   登录用户 ID
     * @param token 登录用户 Token
     * @param id    主键 ID
     * @return ResultEntity
     * @throws Exception
     * @desc 查询详情
     * @author 张国凯
     */
    @RequestMapping("/info")
    @ResponseBody
    public ResultEntity info(@RequestParam(name = "uid", defaultValue = "") String uid,
                             @RequestParam(name = "token", defaultValue = "") String token,
                             @RequestParam(name = "id", defaultValue = "") String id) throws Exception {
        ResultEntity res = new ResultEntity();
        if (StringUtils.isEmpty(id)) {
            throw new Exception("请选择字典");
        }
        Map map = brandService.info(id);
        res.setData(map);
        res.setSuccess(true);
        res.setMsg("查询详情成功");
        return res;
    }

    /**
     * @param uid   登录用户 ID
     * @param token 登录用户 Token
     * @param id    主键 ID
     * @return ResultEntity
     * @throws Exception
     * @desc 启用品牌
     * @author 张国凯
     */
    @RequestMapping("/start")
    @ResponseBody
    public ResultEntity start(@RequestParam(name = "uid", defaultValue = "") String uid,
                              @RequestParam(name = "token", defaultValue = "") String token,
                              @RequestParam(name = "id", defaultValue = "0") String id) throws Exception {
        ResultEntity res = new ResultEntity();
        brandService.update(id, "1");
        res.setSuccess(true);
        res.setMsg("启用成功");
        return res;
    }
    /**
     * @param uid   登录用户 ID
     * @param token 登录用户 Token
     * @param
     * @return ResultEntity
     * @throws Exception
     * @desc   批量启用品牌
     * @author 徐钦勇
     */
    @RequestMapping("/newstart")
    @ResponseBody
    public ResultEntity newstart(@RequestParam(name = "uid", defaultValue = "") String uid,
                              @RequestParam(name = "token", defaultValue = "") String token,
                              @RequestBody List<Map> data) throws Exception {
        ResultEntity res = new ResultEntity();
        brandService.newupdate(data, "1");
        res.setSuccess(true);
        res.setMsg("启用成功");
        return res;
    }

    /**
     * @param uid   登录用户 ID
     * @param token 登录用户 Token
     * @param id    主键 ID
     * @return ResultEntity
     * @throws Exception
     * @desc 停用品牌
     * @author 张国凯
     */
    @RequestMapping("/stop")
    @ResponseBody
    public ResultEntity stop(@RequestParam(name = "uid", defaultValue = "") String uid,
                             @RequestParam(name = "token", defaultValue = "") String token,
                             @RequestParam(name = "id", defaultValue = "0") String id) throws Exception {
        ResultEntity res = new ResultEntity();
        brandService.update(id, "0");
        res.setSuccess(true);
        res.setMsg("停用成功！");
        return res;
    }
    /**
     * @param uid   登录用户 ID
     * @param token 登录用户 Token
     * @param
     * @return ResultEntity
     * @throws Exception
     * @desc   批量停用品牌
     * @author 徐钦勇
     */
    @RequestMapping("/newstop")
    @ResponseBody
    public ResultEntity newstop(@RequestParam(name = "uid", defaultValue = "") String uid,
                                 @RequestParam(name = "token", defaultValue = "") String token,
                                 @RequestBody List<Map> data) throws Exception {
        ResultEntity res = new ResultEntity();
        brandService.newupdate(data, "0");
        res.setSuccess(true);
        res.setMsg("停用成功");
        return res;
    }
    /**
     * @param uid   登录用户 ID
     * @param token 登录用户 Token
     * @param brand 实体对象
     * @return ResultEntity
     * @throws Exception
     * @desc 保存添加或者修改的实体内容，通过主键判断
     * @author 张国凯
     */
    @RequestMapping("/save")
    @ResponseBody
    public ResultEntity save(@RequestParam(name = "uid", defaultValue = "") String uid,
                             @RequestParam(name = "token", defaultValue = "") String token,
                             @RequestBody TBcBrand brand) throws Exception {

        //判断必填项,
        EntityUtils.valid(brand);
        TBcBrand bcBrand = brandService.saveOrUpdate(brand);
        ResultEntity res = new ResultEntity();
        res.setData(bcBrand);
        res.setSuccess(true);
        res.setMsg("保存成功！");
        return res;
    }


    /**
     * @return List
     * @throws Exception
     * @desc 品牌下拉菜单
     * @author 张国凯
     */
    @RequestMapping("/combo")
    @ResponseBody
    public List<Map> combo() throws Exception {
        return this.brandService.combo();
    }

    /**
     * @return Map
     * @throws Exception
     * @desc 品牌编码转换
     * @author 张国凯
     */
    @RequestMapping("/convert")
    @ResponseBody
    public Map convert() throws Exception {
        return this.brandService.convert();
    }
    /**
     * @Name: 图片保存到数据库
     * @Author: dengbin
     * @Date: 2017/11/22 09:57
     * @Remark:
     */

    @RequestMapping("/writeImage")
    public Map writeImage(@RequestParam(name = "uid", defaultValue = "") String uid,
                          @RequestParam(name = "token", defaultValue = "") String token,
                          @RequestParam(name = "image", defaultValue = "") MultipartFile image,
                          @RequestParam(name = "time", defaultValue = "") String time,
                          @RequestParam(name = "id", defaultValue = "")  String id) throws Exception{
        Map map = new HashMap();
        brandService.writePic(image,id);
        map.put("success", true);
        map.put("msg", "保存成功！");
        return map;
    }


    /**
     * @Name: 返回照片流
     * @Author: dengbin
     * @Date: 2017/11/22 09:57
     * @Remark:
     */
    @RequestMapping("/readImage")
    public byte[] readImage(@RequestParam(name = "uid", defaultValue = "") String uid,
                            @RequestParam(name = "token", defaultValue = "") String token,
                            @RequestParam(name = "id", defaultValue = "") String id,
                            @RequestParam(name = "time", defaultValue = "") String time) throws Exception{
        byte[] b = brandService.readPic(uid,id);
        return b;
    }
}
